<template>
  <view class="detail-container">
    <!-- 顶部导航栏 -->
    <!-- <view class="nav-bar glass-effect">
      <view class="nav-back" @tap="goBack">
        <text class="iconfont icon-arrow-left">←</text>
      </view>
      <view class="nav-title">{{ detail.title || '详情介绍' }}</view>
      <view class="nav-actions">
        <text class="iconfont icon-share" @tap="shareContent"></text>
      </view>
    </view> -->
    
    <!-- 内容区域 -->
    <scroll-view scroll-y class="detail-scroll">
      <!-- 背景图片 -->
      <image class="bg-image" :src="detail.imgUrl" mode="aspectFill"></image>
      
      <!-- 内容卡片 -->
      <view class="content-card glass-card">
        <!-- 标题区域 -->
        <view class="detail-header">
          <text class="detail-title">{{ detail.title }}</text>
          <view class="detail-meta">
            <text class="detail-date" v-if="detail.updateDate">{{ detail.updateDate }}</text>
            <text class="detail-views" v-if="detail.views">{{ detail.views }}次浏览</text>
            <view class="detail-tags" v-if="detail.tags && detail.tags.length">
              <text class="detail-tag" v-for="(tag, index) in detail.tags" :key="index">{{ tag }}</text>
            </view>
          </view>
        </view>
        
        <!-- 图片区域 -->
        <view class="detail-image-container">
          <image 
            class="detail-image" 
            :src="detail.imgUrl" 
            mode="widthFix"
          ></image>
          
          <!-- 二维码覆盖层 -->
          <view class="qrcode-overlay" v-if="detail.qrcode">
            <image 
              class="overlay-qrcode" 
              :src="detail.qrcode" 
              mode="aspectFit"
            ></image>
            <view class="overlay-hint">
              <text class="hint-text">长按二维码可直接扫描</text>
            </view>
          </view>
        </view>
        
        <!-- 内容区域 -->
        <view class="detail-content">
          <!-- 使用u-parse组件渲染富文本内容 -->
          <u-parse :content="detail.remark || ''"></u-parse>
        </view>
      </view>
      
      <!-- 底部安全区域 -->
      <view style="height: 120rpx;"></view>
    </scroll-view>
    
    <!-- 二维码弹窗 -->
    <view class="qrcode-modal" v-if="showQrCode" @tap="hideQrCodeModal">
      <view class="qrcode-content glass-card" @tap.stop>
        <view class="qrcode-title">扫描二维码</view>
        <image class="qrcode-image" :src="detail.qrcode" mode="aspectFit"></image>
        <view class="qrcode-desc">扫描上方二维码获取更多信息</view>
        <button class="qrcode-save-btn glass-button" @tap="saveQrCode">保存到相册</button>
        <view class="qrcode-close" @tap="hideQrCodeModal">关闭</view>
      </view>
    </view>
    
    <!-- 加载中提示 -->
    <view class="loading-container" v-if="loading">
      <view class="loading-spinner"></view>
    </view>
  </view>
</template>

<script>
import {detail} from "@/api/pt";
export default {
  data() {
    return {
      // 详情数据
      detail: {},
      // 详情ID
      id: '',
      // 类型：0-团签介绍，1-旅游攻略
      type: 0,
      // 交互状态
      isLiked: false,
      isCollected: false,
      likeCount: 0,
      commentCount: 0,
      // 二维码弹窗
      showQrCode: false,
      // 加载状态
      loading: false
    };
  },
  
  computed: {
    primaryActionText() {
      return this.type === 0 ? '立即咨询' : '查看路线';
    }
  },
  
  onLoad(options) {
    // 获取参数
    if (options.id) {
      this.id = options.id;
    } else {
      uni.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        uni.navigateBack();
      }, 1500);
      return;
    }
    
    if (options.type) {
      this.type = parseInt(options.type);
    }
    
    // 获取详情数据
    this.getDetailData();
  },
  
  methods: {
    // 获取详情数据
    getDetailData() {
      this.loading = true;
      
      // 调用接口获取详情数据
      detail(this.id).then(res => {
        if (res.code === 200) {
          this.detail = res.data;
        } else {
          uni.showToast({
            title: res.msg || '获取详情失败',
            icon: 'none'
          });
        }
      }).catch(err => {
        console.error('获取详情失败', err);
        uni.showToast({
          title: '获取详情失败，请重试',
          icon: 'none'
        });
      }).finally(() => {
        this.loading = false;
      });
    },
    
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },
    
    // 分享内容
    shareContent() {
      uni.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      });
    },
    
    // 显示二维码弹窗
    showQrCodeModal() {
      if (this.detail.qrcode) {
        this.showQrCode = true;
      } else {
        uni.showToast({
          title: '暂无二维码',
          icon: 'none'
        });
      }
    },
    
    // 隐藏二维码弹窗
    hideQrCodeModal() {
      this.showQrCode = false;
    },
    
    // 保存二维码到相册
    saveQrCode() {
      if (!this.detail.qrcode) {
        uni.showToast({
          title: '二维码不存在',
          icon: 'none'
        });
        return;
      }
      
      uni.showLoading({
        title: '保存中...'
      });
      
      uni.downloadFile({
        url: this.detail.qrcode,
        success: (res) => {
          if (res.statusCode === 200) {
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: () => {
                uni.showToast({
                  title: '保存成功',
                  icon: 'success'
                });
              },
              fail: (err) => {
                console.error('保存失败', err);
                uni.showToast({
                  title: '保存失败，请检查权限设置',
                  icon: 'none'
                });
              }
            });
          }
        },
        fail: (err) => {
          console.error('下载失败', err);
          uni.showToast({
            title: '下载失败，请检查网络',
            icon: 'none'
          });
        },
        complete: () => {
          uni.hideLoading();
        }
      });
    },
    
    // 点赞交互
    toggleLike() {
      this.isLiked = !this.isLiked;
      this.likeCount += this.isLiked ? 1 : -1;
      
      uni.showToast({
        title: this.isLiked ? '已点赞' : '已取消点赞',
        icon: 'none'
      });
    },
    
    // 收藏交互
    toggleCollect() {
      this.isCollected = !this.isCollected;
      
      uni.showToast({
        title: this.isCollected ? '已收藏' : '已取消收藏',
        icon: 'none'
      });
    },
    
    // 导航到相关推荐
    navigateToRelated(item) {
      // 跳转到详情页
      uni.navigateTo({
        url: `/pages/introduce/detail?id=${item.id}&type=${this.type}`
      });
    }
  }
};
</script>

<style>
/* 整体容器 */
.detail-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #050b1f;
  position: relative;
  color: #ffffff;
}

/* 背景图片 */
.bg-image {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.3;
  z-index: 0;
}

/* 玻璃效果基础样式 */
.glass-effect {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
}

.glass-card {
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 24rpx;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
}

/* 导航栏 */
.nav-bar {
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30rpx;
  position: relative;
  z-index: 10;
}

.nav-back {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-title {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 32rpx;
  font-weight: 500;
  color: #ffffff;
  max-width: 400rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-actions {
  display: flex;
  align-items: center;
}

/* 内容区域 */
.detail-scroll {
  flex: 1;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.content-card {
  margin: 30rpx;
  padding: 30rpx;
  position: relative;
  z-index: 2;
}

.detail-header {
  margin-bottom: 30rpx;
}

.detail-title {
  font-size: 40rpx;
  font-weight: 600;
  color: #ffffff;
  line-height: 1.4;
  margin-bottom: 20rpx;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 20rpx;
}

.detail-date {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.7);
  margin-right: 20rpx;
}

.detail-views {
  font-size: 26rpx;
  color: rgba(255, 255, 255, 0.7);
}

.detail-tags {
  display: flex;
  flex-wrap: wrap;
  margin-left: auto;
}

.detail-tag {
  font-size: 24rpx;
  color: #ffffff;
  background-color: rgba(24, 144, 255, 0.3);
  padding: 6rpx 16rpx;
  border-radius: 20rpx;
  margin-left: 12rpx;
  margin-bottom: 8rpx;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

/* 图片区域 */
.detail-image-container {
  position: relative;
  width: 100%;
  margin-bottom: 30rpx;
  border-radius: 16rpx;
  overflow: hidden;
}

.detail-image {
  width: 100%;
  display: block;
  border-radius: 16rpx;
}

/* 二维码覆盖层 */
.qrcode-overlay {
  position: absolute;
  bottom: 20rpx;
  right: 20rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  padding: 20rpx;
  border-radius: 16rpx;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.overlay-qrcode {
  width: 160rpx;
  height: 160rpx;
  background-color: #ffffff;
  border-radius: 8rpx;
  margin-bottom: 10rpx;
}

.overlay-hint {
  width: 100%;
  text-align: center;
}

.hint-text {
  font-size: 22rpx;
  color: #FFFFFF;
}

/* 内容区域 */
.detail-content {
  padding: 20rpx 0;
}

/* 二维码弹窗 */
.qrcode-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

.qrcode-content {
  width: 600rpx;
  padding: 40rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.qrcode-title {
  font-size: 34rpx;
  font-weight: 500;
  color: #FFFFFF;
  margin-bottom: 30rpx;
}

.qrcode-image {
  width: 400rpx;
  height: 400rpx;
  margin-bottom: 20rpx;
  border-radius: 20rpx;
  border: 2px solid rgba(255, 255, 255, 0.2);
}

.qrcode-desc {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 30rpx;
  text-align: center;
}

.glass-button {
  background: rgba(24, 144, 255, 0.4);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #FFFFFF;
  border-radius: 40rpx;
}

.qrcode-save-btn {
  width: 80%;
  height: 80rpx;
  line-height: 80rpx;
  font-size: 28rpx;
  margin-bottom: 20rpx;
}

.qrcode-close {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.7);
  padding: 10rpx 20rpx;
  margin-top: 10rpx;
}

/* 加载中样式 */
.loading-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.loading-spinner {
  width: 60rpx;
  height: 60rpx;
  border: 4rpx solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #ffffff;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>

